﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Wormhole.UI.Application.Dialogs.JoinGame
{
	/// <author>Hannes Foulds</author>
	/// <date>22 September 2008</date>
	/// <summary>
	/// Interaction logic for CreateGame.xaml
	/// </summary>
	public partial class CreateGame : Window
	{
		#region Properties
		
		#region Game
		private Engine.Game _game;
		/// <summary>
		/// The Game Engine object.
		/// </summary>
		public Engine.Game Game
		{
			get
			{
				return this._game;
			}

			set
			{
				this._game = value;
				this.Game.JoinResponse += new Wormhole.Engine.Communication.JoinResponseDelegate(Game_JoinResponse);
			}
		}
		#endregion

		#region Joined
		/// <summary>
		/// Was the game joined.
		/// </summary>
		public bool Joined { get; set; }
		#endregion

		#region Description
		/// <summary>
		/// The description of the new game.
		/// </summary>
		public string Description 
		{
			get { return this.txtDescription.Text; }
			set { this.txtDescription.Text = value; }
		}
		#endregion

		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a default instance of the class.
		/// </summary>
		public CreateGame()
		{
			InitializeComponent();
		}
		#endregion

		#region Event Handlers

		#region Create Click
		/// <summary>
		/// Create Click
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnCreate_Click(object sender, RoutedEventArgs e)
		{
			this.CreateNewGame();
		}
		#endregion

		#region Cancel Click
		/// <summary>
		/// Cancel Click
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			this.Cancel(true);
		}
		#endregion

		#region Join Response
		/// <summary>
		/// Handle a join response.
		/// </summary>
		/// <param name="game"></param>
		/// <param name="accepted"></param>
		/// <param name="message"></param>
		private void Game_JoinResponse(Wormhole.Engine.Communication.Game game, bool accepted, string message)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate() {
					if (accepted)
					{
						this.GameJoined();
					}
			});
		}
		#endregion

		#endregion

		#region On Closing
		/// <summary>
		/// Handle the closing event.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			if (!this.Joined)
				this.Cancel(false);

			base.OnClosing(e);
		}
		#endregion

		#region Create New Game
		/// <summary>
		/// Create a new game.
		/// </summary>
		protected void CreateNewGame()
		{
			// disable the input controls
			this.btnCreate.IsEnabled = false;
			this.txtDescription.IsEnabled = false;

			// create the new game
			this.Game.CreateGame(this.Description);
		}
		#endregion

		#region Cancel
		/// <summary>
		/// Cancel the game creation.
		/// </summary>
		/// <param name="close">Should the dialog be closed.</param>
		protected void Cancel(bool close)
		{
			this.Joined = false;
			this.btnCreate.IsEnabled = true;
			this.txtDescription.IsEnabled = true;
			this.Game.CloseGame();

			// close the dialog if needed
			if (close)
			{
				this.Close();
			}
		}
		#endregion

		#region Game Joined
		/// <summary>
		/// Handle the game joined event.
		/// </summary>
		protected void GameJoined()
		{
			this.Joined = true;
			this.Close();
		}
		#endregion
	}
}
